package com.emergency.embackend.service.impl;

import com.emergency.embackend.entity.Quiz;
import com.emergency.embackend.entity.QuizHistory;
import com.emergency.embackend.mapper.QuizDataMapper;
import com.emergency.embackend.mapper.QuizSuitDataMapper;
import com.emergency.embackend.mapper.QuizSuitMapper;
import com.emergency.embackend.param.AnswerParam;
import com.emergency.embackend.param.QuizSubmitParam;
import com.emergency.embackend.param.QuizSuitParam;
import com.emergency.embackend.service.QuizDataService;
import com.emergency.embackend.service.QuizHistoryService;
import com.emergency.embackend.utils.TypeUtils;
import com.emergency.embackend.utils.UserHolder;
import com.emergency.embackend.vo.QuizSubmitVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class QuizDataServiceImpl implements QuizDataService {
    @Autowired
    private QuizDataMapper quizDataMapper;
    @Autowired
    private QuizHistoryService quizHistoryService;
    @Autowired
    private QuizSuitDataMapper quizSuitDataMapper;
    @Autowired
    private QuizSuitMapper quizSuitMapper;
    @Override
    public void deleteByTypeId(Long typeId) {
        quizDataMapper.deleteByTypeId(typeId);
    }

    @Override
    public List getQuizDataBySuitId(Long quizSuitId) {
        //根据套题id获取相应的题目
        List<Long> ids = quizSuitDataMapper.getDataIds(quizSuitId);
        List<Quiz> resList  = quizDataMapper.page(ids);
        return resList;
    }

    @Override
    public void update(Quiz quiz) {
        quizDataMapper.update(quiz);
    }

    @Override
    public void add(Quiz quiz) {
        quizDataMapper.add(quiz);
    }

    @Override
    public void delete(Long id) {
        quizDataMapper.delete(id);
    }

    @Override
    public QuizSubmitVO submit(QuizSubmitParam param) {
        int dataNum = param.getAnswers().size();
        Long userId= UserHolder.getUser().getId();
        Long score = 0L;
        //获取用户答案
        Map<Long, String> userAnswerMap = param.getAnswers()
                .stream()
                .collect(Collectors.toMap(AnswerParam::getQuizDataId, AnswerParam::getAnswer));
        //获取正确答案
        List<Long> ids =param.getAnswers().stream().map(e->e.getQuizDataId()).collect(Collectors.toList());
        List<Quiz> quizDataList = quizDataMapper.page(ids);
        Map<Long, String> correctAnswer = quizDataList
                .stream()
                .collect(Collectors.toMap(Quiz::getId, Quiz::getCorrectJudge));
        for(Map.Entry<Long,String> entry:correctAnswer.entrySet()){
            Long key = entry.getKey();
            String value = entry.getValue();
            if(userAnswerMap.get(key).equals(value)){
                score++;
            }
        }
        QuizHistory quizHistory = new QuizHistory();
        //做题记录
        Long goal = score*(100/dataNum);
        if((goal)>=TypeUtils.QUIZ_SOCRE){
            quizHistory.setIsPass("0");
        }else{
            quizHistory.setIsPass("1");
        }
        quizHistory.setGoal(goal);
        quizHistory.setQuizSuitId(param.getQuizSuitId());
        Date date = new Date();
        quizHistory.setUpdateTime(date);
        quizHistory.setUserId(userId);
        quizHistory.setCreateTime(date);
        quizHistory.setQuizTypeId(quizSuitMapper.getTypeId(param.getQuizSuitId()));
        quizHistoryService.insert(quizHistory);
        QuizSubmitVO quizSubmitVO = new QuizSubmitVO();
        quizSubmitVO.setQuizList(quizDataList);
        quizSubmitVO.setScore(goal);
        return quizSubmitVO;
    }

    @Override
    public PageInfo pageAll(QuizSuitParam param) {
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<Quiz> resList=quizDataMapper.pageAll(param);
        PageInfo pageInfo = new PageInfo(resList);
        return pageInfo;
    }

}
